package com.orangelife.mobile.property.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.sp.CommSp2;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.StringUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.JumpManager;
import com.orangelife.mobile.myhouse.activity.SelectHouseLocationActivity;
import com.orangelife.mobile.myhouse.entity.LocationBean;
import com.orangelife.mobile.property.biz.PropertyHttpBiz;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 物业缴费-选择房屋
 * Created by wgf on 2016/5/6.
 *
 * @author wgf
 * @since v3.6.4
 */
public class ChooseHouseActivity extends OrangeLifeBaseActivity {

    // TODO: 2016/5/6 抽出 ChooseHouseActivity
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rlSelectCity)
    RelativeLayout rlSelectCity;
    @Bind(R.id.rlSelectComm)
    RelativeLayout rlSelectComm;
    @Bind(R.id.rlSelectBuilding)
    RelativeLayout rlSelectBuilding;
    @Bind(R.id.rlSelectRoom)
    RelativeLayout rlSelectRoom;
    @Bind(R.id.tvCity)
    TextView tvCity;
    @Bind(R.id.tvComm)
    TextView tvComm;
    @Bind(R.id.tvBuilding)
    TextView tvBuilding;
    @Bind(R.id.tvRoomNum)
    TextView tvRoomNum;
    @Bind(R.id.btnGetBill)
    Button btnGetBill;
    private String currentType;
    public static final String PLEASE_SELECT = "请选择";
    private String commID = "";
    private String buildingID = "";
    private String apartmentID = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_property_choose_house);
        ButterKnife.bind(this);
        tvTitle.setText("我的房产");
        initCityInfo();
    }

    private void initCityInfo() {
        String cityName = CommSp2.getCityName();
        String commName = CommSp2.getCommName();
        if (!StringUtil.isBlank(cityName) && !StringUtil.isBlank(commName)) {
            tvCity.setText(cityName);
            commID = CommSp2.getCommID();
            tvComm.setText(commName);
        }
    }

    /** 查询明细之前，先判断是何种物业缴费系统 */
    private void requestPropertyType() {
        if (StringUtil.isContainBlank(commID, buildingID, apartmentID)) {
            ToastHelper.getInstance()._toast("请先选择房屋");
        } else {
            showDialog();
            PropertyHttpBiz.getPropertyType(commID, new VolleyHttpUtil.ResponseCallBack() {
                @Override
                public void handleResponse(JSONObject jsonObject, int i) {
                    closeDialog();
                    jumpToNextPage(PropertyHttpBiz.handlePropertyType(jsonObject));
                }
            });
        }

    }

    private void jumpToSelectLocation(String type, String preValue) {
        Bundle bundle = new Bundle();
        bundle.putString(SelectHouseLocationActivity.INTENT_SELECT_TYPE, type);
        bundle.putString(SelectHouseLocationActivity.INTENT_PRE_VALUE, preValue);
        IntentHelper.getInstances().bundleWithBundleForResult(this, SelectHouseLocationActivity.class, bundle, 0);
        currentType = type;
    }

    private void resetSelected() {
        if (currentType.equals(SelectHouseLocationActivity.TYPE_CITY)) {
            tvBuilding.setText("");
            tvComm.setText("");
            tvRoomNum.setText("");
            commID = "";
            buildingID = "";
            apartmentID = "";
        } else if (currentType.equals(SelectHouseLocationActivity.TYPE_COMM)) {
            buildingID = "";
            apartmentID = "";
            tvBuilding.setText("");
            tvRoomNum.setText("");
        } else if (currentType.equals(SelectHouseLocationActivity.TYPE_BUILDING)) {
            tvRoomNum.setText("");
            apartmentID = "";
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        Bundle bundle = data.getExtras();
        if (bundle == null) {
            return;
        }
        LocationBean lb = (LocationBean) bundle.getSerializable(SelectHouseLocationActivity.INTENT_RESULT);
        setLocationInfo(lb);
    }

    /**
     * 选择位置完成后，将值设到TextView上
     *
     * @param lb 位置信息实体
     */
    private void setLocationInfo(LocationBean lb) {
        if (lb == null || currentType == null) {/*currentType可能是空的*/
            return;
        }
        if (SelectHouseLocationActivity.TYPE_CITY.equals(currentType)) {
            tvCity.setText(lb.getCity());
        } else if (SelectHouseLocationActivity.TYPE_COMM.equals(currentType)) {
            tvComm.setText(lb.getComm());
            commID = lb.getCommID();
        } else if (SelectHouseLocationActivity.TYPE_BUILDING.equals(currentType)) {
            tvBuilding.setText(lb.getBuildingName());
            buildingID = lb.getBuildingID();
        } else {
            tvRoomNum.setText(lb.getApartmentName());
            apartmentID = lb.getApartmentID();
        }
    }

    /**
     * @param type 选择位置的类型=城市、小区、楼栋、房号
     * @return 前一个值，请求需要；
     */
    private String hasSelectPreValue(String type) {
        String building = tvBuilding.getText().toString();
        String comm = tvComm.getText().toString();
        String city = tvCity.getText().toString();
        if (type.equals(SelectHouseLocationActivity.TYPE_ROOM_NUM) && !building.equals(PLEASE_SELECT) && !"".equals(buildingID)) {
            return buildingID;
        } else if (type.equals(SelectHouseLocationActivity.TYPE_BUILDING) && !comm.equals(PLEASE_SELECT) && !"".equals(commID)) {
            return commID;
        } else if (type.equals(SelectHouseLocationActivity.TYPE_COMM) && !city.equals(PLEASE_SELECT) && !"".equals(city)) {
            return city;
        }
        return null;
    }

    @OnClick({R.id.rlSelectCity, R.id.rlSelectComm, R.id.rlSelectBuilding, R.id.rlSelectRoom, R.id.btnGetBill})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlSelectCity:
                jumpToSelectLocation(SelectHouseLocationActivity.TYPE_CITY, null);
                resetSelected();
                break;
            case R.id.rlSelectComm:
                String city = hasSelectPreValue(SelectHouseLocationActivity.TYPE_COMM);
                if (city == null || city.trim().equals("")) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请先选择城市");
                } else {
                    jumpToSelectLocation(SelectHouseLocationActivity.TYPE_COMM, city);
                    resetSelected();
                }
                break;
            case R.id.rlSelectBuilding:
                String comm = hasSelectPreValue(SelectHouseLocationActivity.TYPE_BUILDING);
                if (comm == null) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请先选择小区");
                } else {
                    jumpToSelectLocation(SelectHouseLocationActivity.TYPE_BUILDING, comm);
                    resetSelected();
                }
                break;
            case R.id.rlSelectRoom:
                String roomNum = hasSelectPreValue(SelectHouseLocationActivity.TYPE_ROOM_NUM);
                if (roomNum == null) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请先选择楼栋");
                } else {
                    jumpToSelectLocation(SelectHouseLocationActivity.TYPE_ROOM_NUM, roomNum);
                    resetSelected();
                }
                break;
            case R.id.btnGetBill:
                requestPropertyType();
                break;
        }

    }

    /** 跳转到应缴账单界面(通过按钮点击“查询缴费账单”) */
    private void jumpToNextPage(String billType) {
        /*1=旧的，2=新的*/
        if (!PropertyHttpBiz.isNewBillType(billType)) {
            JumpManager.jumpToOldProperty(this, commID, apartmentID);
        } else {
            JumpManager.jumpToPropertyFeeDetail(commID, apartmentID, "1", this);
        }
//        finish();//不关掉选择房屋界面比较方便
    }
}
